﻿//*基础功能库*/
$.extend({
    //包装后的Ajax
    ajaxPost: function (url, params, successFn, msg, useJson) {
        params = params || {},
        successFn = successFn || function () { };
        msg = msg | '请稍等...';
        $.loading(msg);
        var option = {
            url: url,
            dataType: 'json',
            type: 'POST',
            data: params,
            complete: function (xhr, ts) {
                $.hideLoading();
                xhr = null;
                return;
            },
            statusCode: {
                200: function (result, textStatus, jqXhr) {
                    var headerJson = jqXhr.getResponseHeader("X-Responded-JSON");
                    if (headerJson) {
                        var headerJsonObj = eval('(' + headerJson + ')');
                        if (headerJsonObj.status === 401) {
                            window.location.reload();
                            return false;
                        }
                    }
                    successFn(result);
                    return false;
                },
                //警告
                202: function (result) {
                    var data = eval('(' + result.responseText + ')');
                    if (data.needRedirect) {
                        window.location.href = data.redirectUrl;
                        return;
                    }
                    $.msgWarn(result.message);
                    return;
                },
                303: function (result) {
                    //var data = eval('(' + result.responseText + ')');
                    //window.location.href = data.RedirectUrl;
                },
                302: function (result) {
                    var location = result.getResponseHeader("X-Responded-JSON");
                    window.location.href = location;
                },
                //错误
                400: function (result) {
                    var data = eval('(' + result.responseText + ')');
                    if (data.needRedirect) {
                        $.msgSuccess(data.message ? data.message : '跳转中...');
                        setTimeout(function () {
                            window.location.href = data.redirectUrl;
                        }, 100);
                        return;
                    }
                    $.msgError(data.message);
                    return;
                },
                //警告
                500: function (result) {
                    if (result.statusText === "Internal Server Error") {
                        $.msgWarn("内部服务器错误");
                        return;
                    }
                    var data = eval('(' + result.responseText + ')');
                    $.msgWarn(data.message);
                    return;
                },
                //未实现
                501: function (result) {
                    var data = eval('(' + result.responseText + ')');
                    $.msgError(data.message);
                    return;
                },
                //未登录
                401: function (result) {
                    var url = result['redirectUrl'];
                    if (url) {
                        location.href = url;
                    } else {
                        window.location.reload();
                    }
                    return;
                }
            }
        };
        if (useJson) {
            option.contentType = "application/json";
            option.data = JSON.stringify(params);
        }
        return $.ajax(option);
    },

    //打包Form或者查询的data
    packData: function (selector) {
        var elements = $(selector + " [name]");
        var data = {};
        for (var i = 0; i < elements.length; i++) {
            var el = $(elements[i]);
            var name = el.attr('name');
            var value = el.val();
            if (typeof (value) === 'string') {
                value = $.trim(el.val());
            }
            //select多选的数组
            if ($.isArray(value)) {
                value = value.join(',');
            }
            if (el.is('input') && el.attr('type') === 'checkbox') {
                if (el.is(':checked')) {
                    if (data[name]) {
                        data[name] += ',' + value;
                    } else {
                        data[name] = value;
                    }
                }
                continue;
            }
            data[name] = value;
        }
        return data;
    },

    //清空Form
    clearForm: function (canvas) {
        canvas.find('input').each(function (i, item) {
            if ($(item).attr('type') === 'checkbox') {
                var id = $(item).attr('id');
                if (id) {
                    $('#' + id).iCheck('uncheck');
                }
                $(item).prop('checked', false);
            } else {
                $(item).val('');
            }
        });
        canvas.find('textarea').val('');
    },

    //弹出成功消息
    msgSuccess: function (msg) {
        toastr.clear();
        toastr.options = {
            'closeButton': false,
            'debug': false,
            'newestOnTop': false,
            'progressBar': true,
            'positionClass': 'toast-top-right',
            'preventDuplicates': true,
            'onclick': null,
            'showDuration': '300',
            'hideDuration': '1000',
            'timeOut': '5000',
            'extendedTimeOut': '1000',
            'showEasing': 'swing',
            'hideEasing': 'linear',
            'showMethod': 'fadeIn',
            'hideMethod': 'fadeOut'
        }
        toastr.success(msg);
    },

    //警告
    msgWarn: function (msg) {
        toastr.clear();
        toastr.options = {
            "closeButton": true,
            "debug": false,
            "newestOnTop": false,
            "progressBar": true,
            "positionClass": "toast-top-center",
            "preventDuplicates": false,
            "onclick": null,
            "showDuration": "300",
            "hideDuration": "1000",
            "timeOut": "5000",
            "extendedTimeOut": "1000",
            "showEasing": "swing",
            "hideEasing": "linear",
            "showMethod": "fadeIn",
            "hideMethod": "fadeOut"
        }
        toastr.warning(msg);
    },

    //错误
    msgError: function (msg) {
        toastr.clear();
        toastr.options = {
            "closeButton": true,
            "debug": false,
            "newestOnTop": false,
            "progressBar": true,
            "positionClass": "toast-top-right",
            "preventDuplicates": false,
            "onclick": null,
            "showDuration": "300",
            "hideDuration": "1000",
            "timeOut": "5000",
            "extendedTimeOut": "1000",
            "showEasing": "swing",
            "hideEasing": "linear",
            "showMethod": "fadeIn",
            "hideMethod": "fadeOut"
        }
        toastr.error(msg);
    },

    //消息
    msgInfo: function (msg) {
        toastr.clear();
        toastr.options = {
            "closeButton": false,
            "debug": false,
            "newestOnTop": false,
            "progressBar": true,
            "positionClass": "toast-top-right",
            "preventDuplicates": false,
            "onclick": null,
            "showDuration": "300",
            "hideDuration": "1000",
            "timeOut": "5000",
            "extendedTimeOut": "1000",
            "showEasing": "swing",
            "hideEasing": "linear",
            "showMethod": "fadeIn",
            "hideMethod": "fadeOut"
        }
        toastr.info(msg);
    },

    //加载
    loading: function (msg) {
        if (msg) {
            layer.msg(msg, {
                icon: 16,
                time: 0,
                shade: [0.3, '#000'],
                scrollbar: false
            });
        } else {
            layer.load(1,
            {
                shade: [0.3, '#000']
            });
        }
    },

    //关闭loading
    hideLoading: function () {
        layer.closeAll('loading');
        layer.closeAll('dialog');
    },

    //alert需要用户确认的
    msgAlert: function (msg) {
        layer.open({
            type: 0,
            icon: 0,
            content: msg
        });
    },

    //询问提示框
    confirm: function (msg, callback) {
        layer.confirm(msg, { icon: 3, title: '提示', scrollbar: false }, function (index) {
            callback(true);
            layer.close(index);
        });
    },

    //iCheck多选框
    iCheck: function (selector) {
        $(selector).iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green',
            cursor: true,
            labelHover: true
        });
    }
});
